package com.iplatform.commons;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.cloud.client.serviceregistry.AutoServiceRegistration;

/**
 * @Author: Red
 * @CreateDate: 2021-01
 * @UpdateDate: 2021-01
 * @Description: 解决当打包war包时无法注册到nacos
 **/
public class NacosRegRunner implements ApplicationRunner {

    @Autowired(required = false)
    private AutoServiceRegistration registration;

    @Value("${server.port:8080}")
    private Integer serverPort;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        if (registration != null) {
            Class<?> regClass = registration.getClass();
            if(regClass.getName().equals("com.alibaba.cloud.nacos.registry.NacosAutoServiceRegistration")){
                regClass.getMethod("setPort",int.class).invoke(registration,serverPort);
                regClass.getMethod("start").invoke(registration);
            }
        }
    }

}
